

package finalsoft;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;


public class XMLParse {
	private String sepa = File.separator;
	private String cookie = new String("C:" + sepa + "Documents and Settings" + sepa  + System.getProperty("user.name") + sepa + "soft.xml");
	private File f= new File(cookie);
	
	
	
	/**
	 *  检查系统中存不存在 cookie
	 *  不存在则创建一个
	 * @return 保存在 cookie 中的用户名和密码（无的话返回null）
	 */
	
	public String[] checkXML(){
		
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// 创建文件成功后写入默认信息
			writeIn();
		} 
		
		// 存在文件，但权限受限
		if(!f.canRead() || !f.canWrite()){
			f.setWritable(true);
			f.setReadable(true);
		}
		
		// 检查文件的信息记录
		try {
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
			DocumentBuilder builder=factory.newDocumentBuilder();
			org.w3c.dom.Document dom=builder.parse(f);
			
//			Element user = (Element) dom.getElementsByTagName("user").item(0);
			Element uid = (Element) dom.getElementsByTagName("uid").item(0);
			Element password = (Element) dom.getElementsByTagName("password").item(0);
//			user.getAttribute("autologin");
//			uid.getAttribute("saveuid");
//			password.getAttribute("savepassword");
			String[] str = {uid.getAttribute("value"),password.getAttribute("value")};

			return str;     // 返回 1 表示 checkXML成功
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	
	public void updateXML(String ... str){
		try {
			
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
			DocumentBuilder builder=factory.newDocumentBuilder();
			org.w3c.dom.Document dom=builder.parse(f);
			writeIn();
			switch (str.length)
			{
			case 0:
				f.delete();
				break;
				
			case 2:     // 自动登录
				Element password = (Element) dom.getElementsByTagName("password").item(0);
				password.setAttribute("value", str[1]);
				
			case 1:    // 只保存用户名
				Element uid = (Element) dom.getElementsByTagName("uid").item(0);
				uid.setAttribute("value", str[0]);
				
				TransformerFactory transFactory=TransformerFactory.newInstance();  //创建一个TransformerFactory(转换工厂对象)
				Transformer transformer=transFactory.newTransformer();//创建一个Transformer对象（文件对换对象）
				DOMSource domSource=new DOMSource(dom);//反要转换的Document对象封装到一个DOMSource类中
				FileOutputStream out=new FileOutputStream(f);//
				StreamResult xmlResult=new StreamResult(out);//将转换得到的XML文件保存在StreamResult中
				transformer.transform(domSource,xmlResult);//把节点树转换为XML文件
				break;
				
			default:  ;
			}
			

		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
		
		
	}
	
	
	/**
	 * 创建一个XML文档
	 */
	private void writeIn(){
		
		try {
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
			DocumentBuilder builder=factory.newDocumentBuilder();
			
			org.w3c.dom.Document dom=builder.newDocument();
			dom.setXmlVersion("1.0");
			
			Element root = dom.createElement("root");
			Element user = dom.createElement("user");
			Element uid = dom.createElement("uid");
			Element password = dom.createElement("password");
			
			dom.appendChild(root);
			root.appendChild(user);
			user.appendChild(uid);
			uid.setAttribute("value", "");
			user.appendChild(password);
			password.setAttribute("value", "");
			
			TransformerFactory transFactory=TransformerFactory.newInstance();  //创建一个TransformerFactory(转换工厂对象)
			Transformer transformer=transFactory.newTransformer();//创建一个Transformer对象（文件对换对象）
			DOMSource domSource=new DOMSource(dom);//反要转换的Document对象封装到一个DOMSource类中
			FileOutputStream out=new FileOutputStream(f);//
			StreamResult xmlResult=new StreamResult(out);//将转换得到的XML文件保存在StreamResult中
			transformer.transform(domSource,xmlResult);//把节点树转换为XML文件
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
